package in.muton.edushare.components.calendar.api;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import com.edushare.common.vo.Schedules;
import com.edushare.common.vo.Users;

public class EventHelper {
	public static Date getCurrentEndDate(Date currentDate, Schedules schedule) {
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(currentDate);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);

		GregorianCalendar calOriginalDay = new GregorianCalendar();
		calOriginalDay.setTime(schedule.getSchStart());
		calOriginalDay.set(Calendar.HOUR_OF_DAY, 0);
		calOriginalDay.set(Calendar.MINUTE, 0);
		calOriginalDay.set(Calendar.SECOND, 0);

		long difference = (cal.getTime().getTime())
				- (calOriginalDay.getTime().getTime());
		Date date = new Date(schedule.getSchEnd().getTime() + difference);
		return date;
	}
	
	public static Date getCurrentStartDate(Date currentDate, Schedules schedule){
    	GregorianCalendar cal = new GregorianCalendar();
    	cal.setTime(currentDate);
    	cal.set(Calendar.HOUR_OF_DAY,0);
    	cal.set(Calendar.MINUTE,0);
    	cal.set(Calendar.SECOND,0);
    	
    	GregorianCalendar calOriginalDay = new GregorianCalendar();
    	calOriginalDay.setTime(schedule.getSchStart());
    	calOriginalDay.set(Calendar.HOUR_OF_DAY,0);
    	calOriginalDay.set(Calendar.MINUTE,0);
    	calOriginalDay.set(Calendar.SECOND,0);    	
    	
    	long difference = (cal.getTime().getTime()) - (calOriginalDay.getTime().getTime());
    	Date date = new Date(schedule.getSchStart().getTime()+difference);
    	return date;
	}
	
	public static boolean isEditable(Schedules schedule, Users user){
		if(!(schedule.getCreatingUser() == user))
			return false;
		
		Date current = new Date();
		if(schedule.getFrequency() == Schedules.EVENT_ONCE_ONLY && 
				schedule.getSchEnd().getTime() <  current.getTime())
			return false;
		
		Calendar c = new GregorianCalendar();
		c.add(Calendar.DATE, 1);
		Date tommorow = c.getTime();
		if(schedule.getFrequency() != Schedules.EVENT_ONCE_ONLY &&
				schedule.getRepetitionEnd().getTime() < tommorow.getTime())
			return false;
		return true;
	}
}
